import uView from "uview-ui"
import Schema from "uview-ui/libs/util/async-validator";
import Modal from "@/utils/Modal";

/**
 * uview-ui
 * https://www.uviewui.com/components/npmSetting.html
 *
 * */
export default function installUView(Vue) {
  Vue.use(uView);
  /**
   * 修改uview的配置
   * https://www.uviewui.com/components/setting.html
   * */
  uni.$u.setConfig({
    props: {
      search: {
        showAction: false,
        placeholder: '搜索关键字',
        bgColor: "#F4F9FD",
        height: "calc(68rpx - 2px)"
      },
      input: {
        border: "none",
        placeholder: "请输入",
        inputAlign: "right",
        fontSize: "28rpx",
        placeholderStyle: "color:#acacac;font-size:28rpx"
      },
      button: {
        shape: "circle"
      },
      form: {
        labelStyle: () => {
          return {
            fontSize: "28rpx"
          }
        },
        errorType: "toast",
        labelWidth: "auto"
      },
      formItem: {
        borderBottom: true,

      },
      textarea: {
        border: 'none',
        maxlength: '-1',
        height:140,
        autoHeight:true,
      },
      radio: {
        activeColor: "#CA2C2F"
      }
    }
  })
}

/**
 * 检验表单 基于async-validator
 * https://github.com/yiminghe/async-validator
 * */
export function validate(rules, form) {
  const promise = new Promise((resolve, reject) => {
    const validator = new Schema(rules);
    validator.validate(form, errors => {
      if (errors && errors.length) {
        Modal.toast(errors[0].message);
        reject(errors)
      } else {
        resolve()
      }
    })
  })
  return promise
}
